﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using AppEntity;

namespace AppEntity
{
    public class Base
    {
        /// <summary>
        /// 获取 AppEntityDataBase 文件夹位置。如：d:\AppEntity\AppEntityDataBase。
        /// </summary>
        /// <returns></returns>
        public static string GetDataBasePath()
        {
            return GlobalCache.GetGlobalConfig().SelectSingleNode("/root/database").InnerText;
        }

        /// <summary>
        /// 获取指定 App 的数据库文件路径
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public static string GetDataBaseFilePath(string AppID)
        {
            return GetDataBasePath() + AppID + ".sqlite";
        }

        /// <summary>
        /// 获取 AppEntitySettings 文件夹位置。如：d:\AppEntity\AppEntitySettings。
        /// </summary>
        /// <returns></returns>
        public static string GetSettingsPath()
        {
            return GlobalCache.GetGlobalConfig().SelectSingleNode("/root/settings").InnerText;
        }

        /// <summary>
        /// 获取指定 App 的 appsetting.xml 文件路径。如：d:\AppEntity\AppEntitySettings\100120\appsetting.xml
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public static string GetSettingsPath(string AppID)
        {
            return GlobalCache.GetGlobalConfig().SelectSingleNode("/root/settings").InnerText + AppID+"\\appsetting.xml";
        }
       
        /// <summary>
        /// 从缓存中获取指定 App 的配置文件，如果缓存中不存在此配置，则初始化。
        /// </summary>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public static XmlDocument GetAppSetting(string AppID)
        {
            string SettingFile = GetSettingsPath(AppID);
            if (HttpRuntime.Cache[SettingFile] != null)
            {
                return (XmlDocument)HttpRuntime.Cache[SettingFile];
            }
            XmlDocument dom = new XmlDocument();
            dom.LoadFile(SettingFile);
            System.Web.Caching.CacheDependency Dep = new System.Web.Caching.CacheDependency(SettingFile);
            HttpRuntime.Cache.Add(SettingFile, dom, Dep, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), System.Web.Caching.CacheItemPriority.AboveNormal, null);
            return dom;
        }
    }

}
